package vrp;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import vrp.Vehicle;
import vrp.PMF;

public class VehicleEditServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	
        long keyid = Integer.parseInt(req.getParameter("key"));
        String name = req.getParameter("name");
        double capacity = Double.parseDouble(req.getParameter("capacity"));
        double operatingCost = Double.parseDouble(req.getParameter("operatingCost"));
        int vehicleStatus = Integer.parseInt(req.getParameter("vehicleStatus"));

        try {
            Key key = KeyFactory.createKey(Vehicle.class.getSimpleName(), keyid);
            Vehicle vehicle = pm.getObjectById(Vehicle.class, key);
            
            vehicle.setName(name);
            vehicle.setCapacity(capacity);
            vehicle.setOperatingCost(operatingCost);
            vehicle.setVehicleStatus(vehicleStatus);
        } finally {
            pm.close();
        }
        resp.sendRedirect("/vehicles.jsp");
    }
}